<?php

namespace app\admin\controller;

use app\admin\model\attribute\BaseSpec;
use app\admin\model\Category;
use app\admin\model\goods\Goods as model_goods;
use app\admin\model\goods\GoodsServer;
use app\BaseController;
use app\Request;
use think\response\Json;

class Goods extends BaseController
{



    public function productList(){


        if (\request()->isAjax() && \request()->isGet()) {

            $result = model_goods::GetGoodsList();;

            return $this->success($result['data'],'success',$result['count']);
        }


        return view();

    }

    public function categoryList(){

        $list=Category::categoryList();




        return View('',['list'=>$list]);

    }

    public function editAttr(Request $request){

        $tpl=getStoreAttrTpl(session('store_id'));

        if($request->isPost() && $request->isAjax()){

            $id= input('post.id');

            empty($id) ? $type='add':$type='edit';
            if($type=='add'){
                $result= BaseSpec::insertSpec(input('post.'));
                return json($result);
            }else{

                $result= BaseSpec::modifySpec(input('post.'));
                return json($result);
            }
        }

        $product_id=input('get.id');
        return view($tpl,['product_id'=>$product_id]);
    }


    public function updateField(){

        $result= BaseSpec::where(['id'=>input('id')])->update([input('filed')=>input('filed')]);
        return json($result);
    }


    public function attrDel(): Json
    {


        BaseSpec::where(['id'=>input('id')])->delete();

        return $this->success();
    }

    public function attList(): Json
    {
        $where['product_id']=input('product_id');
       $result= BaseSpec::where($where)->select();
      return $this->success($result);
    }



    /***
     * 添加编辑栏目
     */
    public function editCategory(Request $request){

        $id= input('id');
        if($request->isPost() && $request->isAjax()){

            $id= input('id');

            empty($id) ? $type='add':$type='edit';
            if($type=='add'){
                $result= Category::insertCategory();
                return json($result);
            }else{

                $result= Category::modifyCategory();
                return json($result);
            }
        }

        $info=Category::where(['id'=>$id])->find();
        $info ? $info=$info->getData():$info=[];

        !empty(input('parent_id')) ? $parent_id=input('parent_id'):$parent_id=0;

        empty($id) ? $tpl='add_category':$tpl='edit_category';

        return \view($tpl,['info'=>$info,'parent_id'=>$parent_id,'type'=>input('type')]);
    }

    /***
     * 删除
     */
    public function categoryDel(){

        $result=Category::del();
        return $result;
    }



    /****
     * @param
     * @return Json
     */
    public function updateStatus(){

        $list=model_goods::UpdateStatus();
        return \json($list);
    }


    /*****
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateGoods(){

        $result=model_goods::GoodsDetail();


        return \view();

    }




    /****
     * @param Request $request
     * @return Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */

    public function editGoods(Request $request){

        $id= input('id');
        if($request->isPost() && $request->isAjax()){

            $id= input('id');

            $input=$request->post('');
            unset($input['file']);

            $input['album'] ? $input['album']=explode(',',$input['album']):$input['album']=[];
            empty($id) ? $type='add':$type='edit';
            $result= model_goods::EditGoods($input);
//            if($type=='add'){
//                $result= model_goods::EditGoods($input);
//                return $this->success($result);
//            }else{
//
//                $result= model_goods::updateRepair($input);
//                return $this->success($result);
//            }
            return \json($result);
        }


        $info=model_goods::where(['id'=>$id])->find();


        $albums=implode(',',$info->album);
        $info->albums=$albums;



        empty($id) ? $tpl='add_goods':$tpl='edit_goods';

        $category=Category::categoryList();


        $server=GoodsServer::where(['store_id'=>session('store_id')])->select();

        return view($tpl,['info'=>$info,'category'=>$category,'server'=>$server]);
    }


    /****
     * 删除商品
     * @return Json
     */
    public function doodsDel(){

        $list=model_goods::GoodsDel();
        return \json($list);
    }


    /****
     * @inheritDoc 购物车产品
     * @return \think\response\View
     */
    public function CatGoods(){



        return \view('');
    }



    public function goodsOneDetail(){

        $info=model_goods::GoodsGroupDetail();

        return \view('',['info'=>$info]);

    }

    public function goodsDetailInfo(){

        $result=model_goods::GoodsDetail();
        $data = array(
            'code' => 0,
            'msg' => '成功!',
            'count'=>count($result['list']),
            'data'=>$result['list']
        );
        return \json($data);
    }


    public function goodsServer(){


        if (\request()->isAjax() && \request()->isGet()) {

            $result = GoodsServer::getList();

            return $this->success($result);
        }
        return view();
    }


    public function editGoodsServer(Request $request){

        $id= input('id');
        if($request->isPost() && $request->isAjax()){

            $id= input('id');

            $input=$request->post('');
            unset($input['file']);
            empty($id) ? $type='add':$type='edit';
            $result= GoodsServer::EditGoodsServer($input);

            return \json($result);
        }

        $info=GoodsServer::where(['id'=>$id])->find();

        empty($id) ? $tpl='add_goods_server':$tpl='edit_goods_server';


        return view($tpl,['info'=>$info]);
    }

    public function goodServersDel(): Json
    {

        $list=GoodsServer::where(['id'=>input('id')])->delete();

        return $this->success();
    }



}